﻿using System;
using System.Collections;
using System.Collections.Generic;
using App.Common;
using Framework.Core;
using Framework.Utilities;
using UnityEngine;

namespace App.Procedure
{
    /// <summary>
    /// 登录流程。
    /// </summary>
    public class LoginProcedure : ProcedureBase
    {
        private MessageQueue<LoginMsg> _messageQueue;

        /// <summary>
        /// 当进入该流程时执行。
        /// </summary>
        public override void OnEnter()
        {
            base.OnEnter();

            //设置默认的消息队列和UI，并获取消息队列
            _messageQueue = SetDefault<LoginMsg>("LoginUI") as MessageQueue<LoginMsg>;

            //注册监听登录这一项消息
            _messageQueue.RegisterHandler(LoginMsg.Login, Login);
        }

        /// <summary>
        /// 登录的回调
        /// </summary>
        /// <param name="sender">发送者</param>
        /// <param name="args">The <see cref="EventArgs"/> instance containing the event data.</param>
        public void Login(object sender, EventArgs args)
        {
            LoginEventArgs loginArg = args as LoginEventArgs;

            //登录成功或者失败，这里应该是向服务器发送数据并等待回调
            if (loginArg.UserName == "123" && loginArg.Password == "456")
            {
                //保存用户信息
                Data.Instance.UserName = loginArg.UserName;
                Data.Instance.Password = loginArg.Password;

                //登录成功
                Launcher.Instance.StartCoroutine(LoginSuccess());
            }
            else
            {
                //登录失败
                Launcher.Instance.StartCoroutine(LoginFail());
            }
        }

        /// <summary>
        /// 登录成功回调。
        /// </summary>
        /// <returns></returns>
        private IEnumerator LoginSuccess()
        {
            //等待一秒钟假装一下
            yield return new WaitForSeconds(1);

            LoginReplyEventArgs reply = new LoginReplyEventArgs()
            {
                Success = true,
                Message = Localization.Instance.GetText("LoginSuccess")
            };

            _messageQueue.DispatchMessage(LoginMsg.LoginReply, this, reply);

            //切换到下一个流程
            ProcedureManager.Instance.ChangeTo<GuideProcedure>();
        }

        /// <summary>
        /// 登录失败回调。
        /// </summary>
        /// <returns></returns>
        private IEnumerator LoginFail()
        {
            //等待一秒钟假装一下
            yield return new WaitForSeconds(1);

            LoginReplyEventArgs reply = new LoginReplyEventArgs()
            {
                Success = false,
                Message = Localization.Instance.GetText("LoginFail")
            };
            _messageQueue.DispatchMessage(LoginMsg.LoginReply, this, reply);
        }
    }

    /// <summary>
    /// 登录消息类型枚举。
    /// </summary>
    public enum LoginMsg
    {
        None,
        Login,
        LoginReply
    }

    /// <summary>
    /// 登录事件参数。
    /// </summary>
    public class LoginEventArgs : EventArgs
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 密码。
        /// </summary>
        public string Password { get; set; }
    }

    /// <summary>
    /// 登录的回复事件参数，即服务器消息
    /// </summary>
    public class LoginReplyEventArgs : EventArgs
    {
        /// <summary>
        /// 是否登录成功。
        /// </summary>
        public bool Success;

        /// <summary>
        /// 附带的消息之类的。
        /// </summary>
        public string Message;
    }
}